﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    public class Over25 : BaseRule
    {

        double MIN_ODDS = 1.5;
        double MAX_ODDS = 2;
        double MIN_MATCHED = 2000;

        double MIN_OU15_MATCHED = 2000;

        public Over25(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices) : base()
        {

            MarketsUsedInside = new string[] { "over/under 2.5 goals", "Over/Under 1.5 goals" };


            try
            {
                MIN_ODDS = Convert.ToDouble(config["Rule.Over25.MIN_BACK_ODDS"]);
                MAX_ODDS = Convert.ToDouble(config["Rule.Over25.MAX_BACK_ODDS"]);
                MIN_MATCHED = Convert.ToDouble(config["Rule.Over25.MIN_MATCHED"]);
                MIN_OU15_MATCHED = Convert.ToDouble(config["Rule.Over25.MIN_OU15_MATCHED"]);
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
            }


            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for trading O1.5 based on O2.5 @ odds " + MIN_ODDS + " to " + MAX_ODDS, true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; } 


            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game  in games)
            {
                try
                {
                    MyMarket market = FindSoccerGames.GetMarket(game, "Over/Under 2.5 goals");
                    MarketPricesCompressed mpc = marketprices[market.marketId];

                    MarketPricesCompressed mpc_ou15 = marketprices[FindSoccerGames.GetMarket(game, "Over/Under 1.5 goals").marketId];


                    double matched = mpc.TotalMatched;
                    double matched_ou15 = mpc_ou15.TotalMatched;
                    double backodds = mpc.GetBestBackOdds(1);

                    if (backodds >= MIN_ODDS && backodds <= MAX_ODDS && matched >= MIN_MATCHED && matched_ou15 >= MIN_OU15_MATCHED)
                    {
                        Logger.Log(market.marketTime + ", " + BF.convertPathToGame(game) + ", odds for Over 2.5: " + backodds + ", matched on O/U 1.5 " + config["CURRENCY_SIGN"] + matched_ou15, true, true);
                        selectionCount++;
                    }
                }
                catch (Exception ex)
                {

                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);
                }


            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }

        }

    }
}
